//
//  BaseController.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 12/24/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "BaseController.h"
#import "MenuMediator.h"
#import "BadgeButton.h"
#import "NotificationsController.h"

@interface BaseController ()

@end

@implementation BaseController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initAsRootOfNavigationController
{
    self = [super init];
    
    if (self)
    {
        [self.view addGestureRecognizer:[MenuMediator shared].slidingController.panGesture];
        
        [self setLeftButtonType:BarButtonMenu];
        
        [self setRightButtonType:BarButtonBadge];
    }
    
    return self;
}

- (id)initAsSubController
{
    self = [super init];
    
    if (self)
    {
        _swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureHandle:)];
        _swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:_swipeGesture];
    }
    
    return self;
}

- (void)setLeftButtonType:(BarButton)buttonType
{
    if (_leftBarButton == nil)
    {
        _leftBarButton = [[UIBarButtonItem alloc] init];
    }
    
    _leftBarButton.customView = [self newButtonWithType:buttonType];
    
    self.navigationItem.leftBarButtonItem = _leftBarButton;
}

- (void)setRightButtonType:(BarButton)buttonType
{
    if (_rightBarButton == nil)
    {
        _rightBarButton = [[UIBarButtonItem alloc] init];
    }
    
    _rightBarButton.customView = [self newButtonWithType:buttonType];
    
    self.navigationItem.rightBarButtonItem = _rightBarButton;
}

- (UIButton*)newButtonWithType:(BarButton)buttonType
{
    UIButton *button = nil;
    
    if (buttonType == BarButtonBack)
    {
        button = [self newButtonWithImage:[UIImage imageNamed:@"icon_back.png" color:COLOR_BLUE_IOS_7]];
    }
    else if (buttonType == BarButtonSave)
    {
        button = [self newButtonWithTitle:NSLocalizedString(@"LƯU", nil)];
    }
    else if (buttonType == BarButtonClose)
    {
        button = [self newButtonWithTitle:NSLocalizedString(@"LƯU", nil)];
    }
    else if (buttonType == BarButtonUpdate)
    {
        button = [self newButtonWithTitle:NSLocalizedString(@"LƯU", nil)];
    }
    else if (buttonType == BarButtonDone)
    {
        button = [self newButtonWithTitle:NSLocalizedString(@"LƯU", nil)];
    }
    else if (buttonType == BarButtonBadge)
    {
        if (_badgeButton == nil)
        {
            //right button
            _badgeButton = [[BadgeButton alloc] init];
            _badgeButton.frame = RECT(0, 0, 30, 30);
            _badgeButton.tag = BarButtonBadge;
            [_badgeButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        button = _badgeButton;
    }
    else if (buttonType == BarButtonMenu)
    {
        button = [self newButtonWithImage:[UIImage imageNamed:@"icon_menu.png" color:COLOR_BLUE_IOS_7]];
    }
    
    button.tag = buttonType;
    
    return button;
}

- (UIButton*)newButtonWithTitle:(NSString*)title
{
    return [UIButton buttonWithTitle:title target:self selector:@selector(buttonPressed:)];;
}

- (UIButton*)newButtonWithImage:(UIImage*)image
{
    UIButton *button = [UIButton buttonWithImage:image target:self selector:@selector(buttonPressed:)];
    button.frame = RECT(0, 0, 30, 30);
    
    return button;
}

#pragma mark - Swipe gesture delegate
- (void)swipeGestureHandle:(UISwipeGestureRecognizer*)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Button delegate
- (void)buttonPressed:(UIButton*)button
{
    if (button.tag == BarButtonBadge)
    {
        NotificationsController *notificationController = [[NotificationsController alloc] initAsSubController];
        [self pushController:notificationController animated:YES];
    }
    else if (button.tag == BarButtonMenu)
    {
        [self.slidingViewController anchorTopViewTo:ECRight];
    }
    else if (button.tag == BarButtonBack)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    [self navigationButtonPressed:button.tag];
}

- (void)navigationButtonPressed:(BarButton)buttonType
{
    
}

- (void)pushController:(BaseController *)controller animated:(BOOL)animated
{
    [self.navigationController pushViewController:controller animated:animated];
    [controller setLeftButtonType:BarButtonBack];
}

- (void)addBottomToolbarWithTitles:(NSString *)title, ...
{
    if (_bottomToolbar)
    {
        return;
    }
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    va_list args;
    va_start(args, title);
    for (id arg = title; arg != nil; arg = va_arg(args, id))
    {
        [array addObject:arg];
    }
    va_end(args);
    
    // Do more stuff here...
    
    _bottomToolbar = [[BottomToolbar alloc] initWithTitles:array];
    _bottomToolbar.delegate = self;
    [self.view addSubview:_bottomToolbar];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    if (_bottomToolbar)
    {
        _bottomToolbar.frame = RECT(0, self.view.height - HEI_HEADER_VIEW, self.view.width, HEI_HEADER_VIEW);
    }
}

#pragma mark - Bottom toobar delegate

- (void)bottomToolbar:(BottomToolbar *)toolbar didPressedButtonAtIndex:(NSInteger)index
{
    
}
@end
